package com.hs.pre.search;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.hs.Environment;
import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;
import com.hs.pre.model.ClomnObj;

public class PersonConfig {
	private static PersonConfig inst = new PersonConfig();
	private static Map PARAM_CONFIG_SEARCH = new HashMap();
	private static Map PARAM_CONFIG_VIEW = new HashMap();
	private PersonConfig(){
		if(null == PARAM_CONFIG_SEARCH){
			PARAM_CONFIG_SEARCH = new HashMap(); 
		}
		if(null == PARAM_CONFIG_VIEW){
			PARAM_CONFIG_VIEW = new HashMap();
		}
	}
	private static PersonConfig getConfigInstance(){
		if(null == inst)
			inst = new PersonConfig();
		return inst;
	}
	public static void test(String str){
		System.out.println("set:" + str);
	}
	public static String getSearchConfig(String userId,String key){
		System.out.println("key:::" + key);
		PersonConfig instance = getConfigInstance();
		String keyValue = null;
		if(StringUtils.isEmpty(userId)){ 
			return keyValue;
		}
		DBOperator dbo = null;
		try {
			dbo = DatabaseConnection.getDBOperator();
		} catch (Exception e) { 
		}
		String sql = "select * from "+Environment.getValue(Environment.PROP_DB_SCHEMA)+".pre_profile where loginId=? ";
		List list = dbo.select(sql, new Object[]{userId});
		 
		if(null == list || list.size() <=0){
			instance.initSearchDefaultConfig(userId);
			keyValue = getSearchConfig(userId,key);
		}else{
			ClomnObj obj = new ClomnObj();
			Map config = new HashMap();
			for(int i = 0;i<list.size();i++){
				Map row = (Map)list.get(i);
				//if(StringUtils.isNotEmpty(row.get("configStr")+"")){
					config.put(row.get("CONFIGSTR")+"", row.get("CONFIGSTR"));
				//}
			} 
			obj.setConfigMap(config);
			PARAM_CONFIG_SEARCH.put(userId, obj);
		}
		ClomnObj obj1 = (ClomnObj)PARAM_CONFIG_SEARCH.get(userId); 
		System.out.println(PARAM_CONFIG_SEARCH+";;;;");
		keyValue = obj1.getConfigMap().get(key)+"";
		System.out.println("key:" + key + "keyVALUE:" + keyValue);
		return keyValue;
	}
	private void initSearchDefaultConfig(String userId){
		String sql = "INSERT INTO "+Environment.getValue(Environment.PROP_DB_SCHEMA)+".PRE_PROFILE (LOGINID,CONFIGSTR,CONFIGTYPE,ORDERNUM) VALUES (?,?,?,?)";
		DBOperator dbo = null;
		try {
			dbo = DatabaseConnection.getDBOperator();
			dbo.update(sql, new Object[]{userId,"laiy",1,1});
			dbo.update(sql, new Object[]{userId,"gongxiao",1,2});
			dbo.update(sql, new Object[]{userId,"zucheng",1,3});
			dbo.update(sql, new Object[]{userId,"zhuzhi",1,4});
		} catch (Exception e) { 
		}
	}

}
